package ljl.alg.jianzhioffer.round3.medium;

import commons.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class _34_pathSum {

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root, target);
        return res;
    }
    void dfs(TreeNode root, int rest) {
        if (root == null) return;
        path.add(root.val);
        rest -= root.val;
        if (rest == 0 && root.left == null && root.right == null) {
            res.add(new ArrayList<>(path));
        } else {
            dfs(root.left, rest);
            dfs(root.right, rest);
        }
        path.remove(path.size() - 1);
    }
}
